import type { ComponentCode, ComponentMeta } from '../type'
import { cloneDeep } from '@/utils/helper'
import { buildShortUUID } from '@/utils/uuid'
import { componentDefinitionMap, componentPropertyMap } from '../component-definition'
import { withDefaultProperty } from './withDefaultProperty'

export function createComponent<ComponentConfig>(code: ComponentCode): ComponentMeta<ComponentConfig> {
  return {
    id: buildShortUUID(),
    config: withDefaultProperty(code, cloneDeep(componentPropertyMap.get(code)!.property)),
    definition: componentDefinitionMap.get(code)!.definition,
  }
}
